package net.tngou.iask.dao;

import java.util.ArrayList;
import java.util.List;

import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.util.StringUtil;

/**
 * 患者
 * @author tngou@tngou.net
 *
 */
public class PatientDao extends BaseDao {
	/**
	 * 取得患者信息
	 * @param page 当前页
	 * @param size 页大小
	 * @param main 实体
	 * @param keyword 关键词
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,String keyword) {
		List<Patient> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT patient.*";
		String sql_t="SELECT COUNT(patient.id) ";
		String sql_o=" ORDER BY patient.id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_patient` patient";
		
		if(StringUtil.isNotEmpty(keyword))
		{
			if(main!=0)
			{
				sql= " FROM `"+POJO.DABASE+"_patient` patient,`"+POJO.DABASE+"_patient_main` patient_main WHERE patient.id=patient_main.patient AND patient_main.main=? ";
				sql=sql+" AND (name LIKE ? OR no LIKE ?)";
				Object[] params={main,keyword+"%",keyword+"%"};
				list=QueryHelper.query_slice(Patient.class, sql_r+sql+sql_o, page, size, params);			
				total=(int) QueryHelper.stat(sql_t+sql, params);
			}else
			{
				sql=sql+"  name LIKE ? OR no LIKE ? ";				
				Object[] params={keyword+"%",keyword+"%"};
				list=QueryHelper.query_slice(Patient.class, sql_r+sql+sql_o, page, size, params);			
				total=(int) QueryHelper.stat(sql_t+sql, params);
			}
			
		}else{
			
			if(main!=0)
			{	
				sql= " FROM `"+POJO.DABASE+"_patient` patient,`"+POJO.DABASE+"_patient_main` patient_main WHERE patient.id=patient_main.patient AND patient_main.main=? ";
				list=QueryHelper.query_slice(Patient.class, sql_r+sql+sql_o, page, size,main);	
				total=(int) QueryHelper.stat(sql_t+sql,main);
			}
			else
			{
				list=QueryHelper.query_slice(Patient.class, sql_r+sql+sql_o, page, size);	
				total=(int) QueryHelper.stat(sql_t+sql);
			}
			
		}
		
		return new PageUtil(list, page, size, total);
	}
	
	
	public PageUtil getPageUser(int page,int size,long user,String keyword) {
		List<Patient> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT patient.*";
		String sql_t="SELECT COUNT(patient.id) ";
		String sql_o=" ORDER BY patient.id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_patient` patient,(SELECT DISTINCT cure.patient FROM `"+POJO.DABASE+"_cure` cure, `"+POJO.DABASE+"_cure_user` cure_user WHERE cure.id=cure_user.cure AND cure_user.user=?) patient_user WHERE patient.id=patient_user.patient ";
		
		if(StringUtil.isNotEmpty(keyword))
		{
			sql=sql+" AND (name LIKE ? OR no LIKE ? OR phone LIKE ?)";
			Object[] params={user,keyword+"%",keyword+"%",keyword+"%"};
			list=QueryHelper.query_slice(Patient.class, sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
			
		}else{
			list=QueryHelper.query_slice(Patient.class, sql_r+sql+sql_o, page, size,user);	
			total=(int) QueryHelper.stat(sql_t+sql,user);
		}
		
		return new PageUtil(list, page, size, total);
	}
	
	/**
	 * 确定实体下的所有用户
	 * @param main
	 * @return
	 */
	public List<Patient> list(long main,int type) {
		String sql_r="SELECT patient.*";
		String sql= " FROM `"+POJO.DABASE+"_patient` patient,`"+POJO.DABASE+"_patient_main` patient_main WHERE patient.id=patient_main.patient AND patient_main.main=? "
				+ " patient.id NOT IN(SELECT patient FROM `"+POJO.DABASE+"_access` WHERE type=?) ";
		String sql_o=" ORDER BY patient.id DESC";//排序
		return QueryHelper.query(Patient.class, sql_r+sql+sql_o, main,type);
	}
}
